package com.wanyang.service;

import com.wanyang.aliyun.service.OssService;
import com.wanyang.vo.UploadVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class CommonServiceImpl implements CommonService {
    @Autowired
    private OssService ossService;

    @Override
    public UploadVO uploadImg(MultipartFile multipartFile) {
        String url = null;
        try {
            url = ossService.upload(multipartFile.getInputStream(), "image/jpg", "default");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return new UploadVO(url);
    }

    @Override
    public UploadVO uploadVideo(MultipartFile multipartFile) {
        // TODO 判断文件类型

        String url = ossService.upload(multipartFile);

        String coverUrl = ossService.processVideo(ossService.getObjectName(url));

        return new UploadVO(url, coverUrl);
    }
}
